local huailan = fk.CreateSkill {
  name = "xiaobai__huailan",
}

Fk:loadTranslationTable {
  ["xiaobai__huailan"] = "徊澜",
  [":xiaobai__huailan"] = "你可以将两张牌当【决斗】使用，此牌额定目标数改为转化牌中【杀】的数量。此牌造成伤害时你攻击范围调整至你与受伤角色的距离，若因此减少，你摸一张牌。",
  ["#xiaobai__huailan"] = "徊澜：你可以将两张牌当【决斗】使用",
  ["#xiaobai__huailan_draw"] = "徊澜：攻击范围减少，%from 摸一张牌",
  ["@xiaobai__huailan"] = "徊澜",

  ["$xiaobai__huailan1"] = "百川尽赤、海壑填骸，此非天道昭彰乎？",
  ["$xiaobai__huailan2"] = "总百揆、领六州，孤欲拯济天下疮痍！",
}

huailan:addEffect("viewas", {
  pattern = "duel",
  prompt = "#xiaobai__huailan",
  card_filter = function(self, player, to_select, selected)
    return #selected < 2 and table.contains(player:getCardIds("h"), to_select)
  end,
  view_as = function(self, player, cards)
    if #cards < 2 then return nil end
    local card = Fk:cloneCard("duel")
    card:addSubcards(cards)
    card.skillName = huailan.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:getHandcardNum() >= 2
  end,
  enabled_at_response = function(self, player, response)
    return (not response) and player:getHandcardNum() >= 2
  end,
})

-- 处理造成伤害时的效果
huailan:addEffect(fk.DamageCaused, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and
        data.card and data.card.skillName == huailan.name
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local victim = data.to
    local current_range = player:getAttackRange()
    local distance = player:distanceTo(victim)

    -- 攻击范围调整至距离
    room:setPlayerMark(player, "@xiaobai__huailan", distance)
    room:setPlayerMark(player, "xiaobai__huailan-change", 1)
    -- 若因此减少，摸一张牌
    if distance < current_range then
      player:drawCards(1, huailan.name)
      room:sendLog {
        type = "#xiaobai__huailan_draw",
        from = player.id,
      }
    end
  end
})


huailan:addEffect("targetmod", {
  extra_target_func = function(self, player, skill, card)
    if card and card.name == "duel" and #card.subcards > 0 then
      local slash_count = -1
      for _, cid in ipairs(card.subcards) do
        local c = Fk:getCardById(cid)
        if c.trueName == "slash" then
          slash_count = slash_count + 1
        end
      end
      return slash_count
    end
  end,
})

huailan:addEffect("atkrange", {
  final_func = function(self, player)
    if player:hasSkill(huailan.name) and player:getMark("xiaobai__huailan-change") > 0 then
      return player:getMark("@xiaobai__huailan")
    end
  end,
})

return huailan
